//
//  GPContactTool.m
//  GuaPi
//
//  Created by 陈华 on 2017/6/22.
//  Copyright © 2017年 Joanlove. All rights reserved.
//

#import "GPContactTool.h"


@implementation GPContactTool

/**获取通讯录权限*/
+ (void)requestForAuth:(void(^)(ABAuthorizationStatus status))handler
{
    //1. 获取授权状态
    ABAuthorizationStatus status = ABAddressBookGetAuthorizationStatus();
    
    //2. 创建 AddrssBook
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, NULL);
    
    //3. 没有授权时就授权
    if (status == kABAuthorizationStatusNotDetermined) {
        
        ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
            
            //3.1 返回
            if (handler) {
                handler(ABAddressBookGetAuthorizationStatus());
            }
            
            //3.2 释放
            CFRelease(addressBook);
        });
    }
    //4. 如果已经授权或者拒绝，直接返回
    else{
        //回调
        if (handler) {
            handler(status);
        }
        
        //释放
//        CFRelease(addressBook);
    }
}

/**获取联系人数据*/
+ (void)loadContactData:(void(^)(NSArray *contacts))completion
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        //0 创建数组
        NSMutableArray *arrayM = [NSMutableArray array];
        
        //1 创建通讯录对象
        ABAddressBookRef addressBook = ABAddressBookCreate();
        
        //2 获取所有的联系人
        CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);
        CFIndex peopleCount = CFArrayGetCount(peopleArray);
        
        //3 遍历所有的联系人
        for (int i = 0; i < peopleCount; i++) {
            
            //3.0 定义字典
            NSMutableDictionary *contact = [NSMutableDictionary dictionary];
            
            //3.1 获取某一个联系人
            ABRecordRef person = CFArrayGetValueAtIndex(peopleArray, i);
            
            //3.2 获取联系人的姓名
            NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
            NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
            contact[kContactName] = [NSString stringWithFormat:@"%@%@",lastName.length==0?@"":lastName,firstName.length==0?@"":firstName];
            
            //3.3 获取电话号码
            //--3.3.1 获取所有的电话号码
            ABMultiValueRef phones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            CFIndex phoneCount = ABMultiValueGetCount(phones);
            
            //--3.3.2 遍历拿到每一个电话号码
            NSString *phoneNumbers = @"";
            for (int i = 0; i < phoneCount; i++) {
                //--- 获取电话对应的key
//                NSString *phoneLabel = (__bridge_transfer NSString *)ABMultiValueCopyLabelAtIndex(phones, i);
                
                //--- 获取电话号码
                NSString *phoneValue = (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phones, i);
                
                //--- 拼接电话号码
                if (phoneNumbers.length == 0) {
                    phoneNumbers = phoneValue;
                }else{
                    phoneNumbers = [NSString stringWithFormat:@"%@,%@",phoneNumbers,phoneValue];
                }
            }
            contact[kContactPhones] = phoneNumbers;
            
            //添加
            [arrayM addObject:contact];
            
            CFRelease(phones);
        }
        
        // 释放不再使用的对象
        CFRelease(addressBook);
        CFRelease(peopleArray);
        
        //回主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            
            //返回
            if (completion) {
                completion(arrayM);
            }
            
        });
        
    });
}



@end
